package com.aaa.sbm.service;

import com.aaa.sbm.constants.ExceptionConstant;
import com.aaa.sbm.constants.ReturnStatus;
import com.aaa.sbm.dao.DeptDao;
import com.aaa.sbm.dao.DeptImgDao;
import com.aaa.sbm.entity.Dept;
import com.aaa.sbm.entity.DeptImg;
import com.aaa.sbm.util.CustomException;
import com.aaa.sbm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;
    @Autowired
    private HandlerFileService handlerFileService;
    @Autowired
    private DeptImgDao deptImgDao;

    @Override
    public Result list() {
        List<Dept> list = deptDao.list();
        if(list==null || list.size()==0){
            throw new CustomException(ExceptionConstant.DATA_NOT_EXIST.getErrorCode(),
                    ExceptionConstant.DATA_NOT_EXIST.getErrorMessage());
        }
        return new Result<List<Dept>>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),list);
    }

    @Override
    public Result page(Map paramMap) {
        Object pageNo = paramMap.get("pageNo");
        Object pageSize = paramMap.get("pageSize");
        if(pageNo==null || pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode()
                    ,ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        int start=(Integer.valueOf(pageNo+"")-1)*Integer.valueOf(pageSize+"");
        int end=Integer.valueOf(pageNo+"")*Integer.valueOf(pageSize+"")+1;
        paramMap.put("start",start);
        paramMap.put("end",end);
        List<Dept> page = deptDao.page(paramMap);
        int i = deptDao.pageCount(paramMap);
        Map map=new HashMap();
        map.put("pageDept",page);
        map.put("total",i);
        return new Result<Map>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),map);
    }

    @Override
    public Result getById(Integer deptNo) {
        if(deptNo==null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        Dept dept = deptDao.getById(deptNo);
        if(dept==null){
            throw new CustomException(ExceptionConstant.DATA_NOT_EXIST.getErrorCode(),
                    ExceptionConstant.DATA_NOT_EXIST.getErrorMessage());
        }
        return new Result<Dept>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),dept);
    }

    @Override
    public Result add(Dept dept) {
        if(dept==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        int add = deptDao.add(dept);
//        int addImg = deptImgDao.add(new DeptImg(dept.getPath(), dept.getDeptNo()));
        if(add<=0){
            throw new CustomException(ExceptionConstant.SYS_EXCEPTION.getErrorCode(),
                    ExceptionConstant.SYS_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),add);
    }

    @Override
    public Result update(Dept dept) {
        if(dept==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        int update = deptDao.update(dept);
        if(update<=0){
            throw new CustomException(ExceptionConstant.SYS_EXCEPTION.getErrorCode(),
                    ExceptionConstant.SYS_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),update);
    }

    @Override
    public Result delete(Integer deptNo) {
        if(deptNo==null){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        int delete = deptDao.delete(deptNo);
        if(delete<=0){
            throw new CustomException(ExceptionConstant.SYS_EXCEPTION.getErrorCode(),
                    ExceptionConstant.SYS_EXCEPTION.getErrorMessage());
        }
        return new Result<Integer>(ReturnStatus.SUCCESS.getReturnCode(),ReturnStatus.SUCCESS.getReturnMsg(),delete);
    }
}
